#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

int main()
{
    const char *filename = "./record.txt";
    int fd = open(filename, O_RDWR | O_CREAT, 0644);
    if (fd == -1)
    {
        perror("open error:");
        exit(-1);
    }
    printf("fd = %d\n", fd);

    /*从文件开头向后移动20字节.*/
    off_t offset = lseek(fd, 50, SEEK_SET);
    printf("offset:%ld\n", offset);

    char buffer[128] = {0};
    read(fd, buffer, sizeof(buffer) - 1);
    printf("buffer = %s\n", buffer);

    /*可通过lseek函数计算得到文件的大小*/
    offset = lseek(fd, 0, SEEK_END);
    printf("offset = %ld\n", offset);

    close(fd);

    return 0;
}